package myuser;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import myPicture.Picture;
import myPicture.PictureDao;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	UserDao dao = new UserDao();
	HttpServletResponse response = ServletActionContext.getResponse();
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();
	PrintWriter out = null;

	private User user;

	public UserDao getDao() {
		return dao;
	}

	public void setDao(UserDao dao) {
		this.dao = dao;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	String idType;

	public String getIdType() {
		return idType;
	}

	public void setIdType(String idType) {
		this.idType = idType;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// 添加用户
	public String add() throws Exception {
		// TODO Auto-generated method stub
		dao.addUser(user);
		// TODO Auto-generated method stub
		return "list"; // 显示最新用户表
	}

	// 判断用户是否已经注册
	public String CheckExists() throws Exception {
		// TODO Auto-generated method stub
		out = response.getWriter();
		if (dao.checkexists(user)) {
			out.print("1");
		} else {
			out.print("0");
		}
		return null;
	}

	// 判断用户是否登录
	public String CheckLogin() throws Exception {
		// TODO Auto-generated method stub
		out = response.getWriter();
		if (dao.checkLogin(user)) {
			session.setAttribute("user", user);// 已登录，保存在session中
			out.print("1");
		} else {
			out.print("0");
		}

		return null;
	}

	// 显示用列表
	public String list() {
		ArrayList<User> list = dao.getusers(); // 用ArrayList接收所有的用户信息
		ActionContext ctx = ActionContext.getContext();
		ctx.put("USERLIST", list);	//将用户保存在Context中,以便显示用户列表使用
		return SUCCESS;
	}

	//修改用户
	public String modify() {
		user = dao.getUserId(user.getId());

		// 加载用户的照片
		PictureDao pdao = new PictureDao();
							//用ArrayList接收用户照片的信息
		ArrayList<Picture> list = pdao.getpicture(user.getId(), idType);
		ActionContext tex = ActionContext.getContext();
		tex.put("PICTURE", list);	//保存在Context中
		return "modify";
	}

	//删除用户
	public String delete() {
		dao.deleteuser(user);
		return null;
	}

	//修改用户之后保存在数据库
	public String save() {
		dao.modifyUser(user);
		return "list";
	}

}
